//
//  XMPWSetView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/6/3.
//

#import "XMPWSetView.h"

@interface XMPWSetView ()

@property (nonatomic, strong) UITextField *pwTextField;
@property (nonatomic, strong) UITextField *codeTextField;

@end

@implementation XMPWSetView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navView.backgroundColor = kColorHEX(0xffffff);
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), kNavagationBarH + KScaleWidth(24.0), self.width - KScaleWidth(28.0), KScaleWidth(24.0)) font:KFontSize(24.0) textColor:kColorHEX(0x221232)];
    titleLabel.text = @"请设置密码" ;
    [self addSubview:titleLabel];
    
    UILabel *explainLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.x, titleLabel.maxY, titleLabel.width, KScaleWidth(46.0)) font:KFontSize(14.0) textColor:kColorHEX(0x988DA2)];
    explainLabel.text = @"密码为8-20位，至少包含字母、数字、符号2种组合";
    [self addSubview:explainLabel];
    
    UIView *inputBgView = [[UIView alloc] initWithFrame:CGRectMake((self.width - KScaleWidth(320.0))/2, explainLabel.maxY + KScaleWidth(90.0), KScaleWidth(320.0), KScaleWidth(44.0))];
    inputBgView.backgroundColor = kColorHEX(0xF5F3F7);
    [self addSubview:inputBgView];
    [inputBgView setCornerRadius:inputBgView.height/2];
    [inputBgView addSubview:self.pwTextField];
//    UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(inputBgView.width - KScaleWidth(40.0) - KScaleWidth(22.0), 0, KScaleWidth(22.0), inputBgView.height) image:@"" selectImg:@""];
    UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(28.0) frame:CGRectMake(inputBgView.width - KScaleWidth(40.0) - KScaleWidth(22.0), 0, KScaleWidth(22.0), inputBgView.height) title:@"×" titleColor:kColorHEX(0xCBC3D2)];
    [clearBtn addTarget:self action:@selector(clearAction:) forControlEvents:UIControlEventTouchUpInside];
    [inputBgView addSubview:clearBtn];
    
    UIButton *canSeeBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(inputBgView.width - KScaleWidth(10.0) - KScaleWidth(28.0), 0, KScaleWidth(28.0), inputBgView.height) image:@"mine_can_see_icon_select" selectImg:@"mine_can_see_icon"];
    [canSeeBtn addTarget:self action:@selector(canSeeAction:) forControlEvents:UIControlEventTouchUpInside];
    [inputBgView addSubview:canSeeBtn];
    
    UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(inputBgView.x + KScaleWidth(10.0), inputBgView.maxY + KScaleWidth(6.0), self.width - inputBgView.x, KScaleWidth(24.0)) font:KFontSize(12.0) textColor:kColorHEX(0xCCCCCC)];
    subTitleLabel.text = @"通过短信验证可以使用新密码";
    [self addSubview:subTitleLabel];
    
    [self addSubview:self.codeTextField];
    _codeTextField.y = subTitleLabel.maxY + KScaleWidth(16.0);
    
    UIButton *getCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(17.0) frame:CGRectMake((self.width - KScaleWidth(320.0))/2, _codeTextField.maxY + KScaleWidth(66.0), KScaleWidth(320.0), KScaleWidth(44.0)) title:@"提交" titleColor:kColorHEX(0x333333)];
    [getCodeBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, .037)];
    [getCodeBtn setCornerRadius:getCodeBtn.height/2];
    [getCodeBtn addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:getCodeBtn];
}

//清理
- (void)clearAction:(UIButton *)button {
    _pwTextField.text = @"";
}

//可查看
- (void)canSeeAction:(UIButton *)button {
    button.selected = !button.selected;
    _pwTextField.secureTextEntry = button.selected;
}

//获取验证码
- (void)getCodeAction:(UIButton *)button {
    self.getCodeBlock ? self.getCodeBlock() : nil;
}

- (void)commitAction:(UIButton *)button {
    if (!_pwTextField.text || _pwTextField.text.length == 0) {
        [MBProgressHUD showError:@"请输入密码"];
        return;
    }
    if (!_codeTextField.text || _codeTextField.text.length == 0) {
        [MBProgressHUD showError:@"请输入验证码"];
        return;
    }
    self.commitBlock ? self.commitBlock(_codeTextField.text, _pwTextField.text) : nil;
}

- (UITextField *)pwTextField {
    if (!_pwTextField) {
        _pwTextField = [[UITextField alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), 0, KScaleWidth(320.0) - KScaleWidth(16.0) - KScaleWidth(60.0), KScaleWidth(44.0))];
        _pwTextField.font = KFontSize(16.0);
        _pwTextField.placeholder = @"请输入密码";
    }
    return _pwTextField;
}

- (UITextField *)codeTextField {
    if (!_codeTextField) {
        _codeTextField = [[UITextField alloc] initWithFrame:CGRectMake((self.width - KScaleWidth(320.0))/2, 0, KScaleWidth(320.0), KScaleWidth(44.0))];
        _codeTextField.font = KFontSize(16.0);
        _codeTextField.backgroundColor = kColorHEX(0xF5F3F7);
        _codeTextField.placeholder = @"请输入验证码";
        _codeTextField.keyboardType = UIKeyboardTypeNumberPad;
        [_codeTextField setCornerRadius:_codeTextField.height/2];
        
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(16.0), _codeTextField.height)];
        _codeTextField.leftViewMode = UITextFieldViewModeAlways;
        _codeTextField.leftView = leftView;
        
        UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScaleWidth(102.0), _codeTextField.height)];
        UIButton *getCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:rightView.bounds title:@"获取验证码 " titleColor:kColorHEX(0xDD4B39)];
        [getCodeBtn addTarget:self action:@selector(getCodeAction:) forControlEvents:UIControlEventTouchUpInside];
        [rightView addSubview:getCodeBtn];
        _codeTextField.rightViewMode = UITextFieldViewModeAlways;
        _codeTextField.rightView = rightView;
    }
    return _codeTextField;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
